package com.smartx.app.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceManager;
import com.smartx.app.R;
import com.smartx.config.Constants;

public class SettingsFragment extends PreferenceFragmentCompat
        implements SharedPreferences.OnSharedPreferenceChangeListener  {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.fragment_settings);
        final Preference wallets = findPreference("pref_wallet");

//        wallets.setOnPreferenceClickListener(preference -> {
//            manageWalletsRouter.open(getActivity(), false);
//            return false;
//        });
//
//        findDefaultWalletInteract
//                .find()
//                .subscribe(wallet -> {
//                    PreferenceManager
//                            .getDefaultSharedPreferences(getActivity())
//                            .edit()
//                            .putString("pref_wallet", wallet.address)
//                            .apply();
//                    wallets.setSummary(wallet.address);
//                });

        final ListPreference listPreference = (ListPreference) findPreference("pref_rpcServer");
        // THIS IS REQUIRED IF YOU DON'T HAVE 'entries' and 'entryValues' in your XML
        setRpcServerPreferenceData(listPreference);
        listPreference.setOnPreferenceClickListener(preference -> {
            setRpcServerPreferenceData(listPreference);
            return false;
        });
        String versionString = getVersion();
        Preference version = findPreference("pref_version");
        version.setSummary(versionString);
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(getActivity());
        preferences
                .registerOnSharedPreferenceChangeListener(SettingsFragment.this);
        final Preference rate = findPreference("pref_rate");
        rate.setOnPreferenceClickListener(preference -> {
            rateThisApp();
            return false;
        });

        final Preference twitter = findPreference("pref_twitter");
        twitter.setOnPreferenceClickListener(preference -> {
            Intent intent;
            try {
                // get the Twitter app if possible
                getActivity().getPackageManager().getPackageInfo("com.twitter.android", 0);
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=911011433147654144"));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            } catch (Exception e) {
                // no Twitter app, revert to browser
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/trustwalletapp"));
            }
            startActivity(intent);
            return false;
        });

        final Preference facebook = findPreference("pref_facebook");
        facebook.setOnPreferenceClickListener(preference -> {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/trustwalletapp"));
            startActivity(intent);
            return false;
        });

//        final Preference donate = findPreference("pref_donate");
//        donate.setOnPreferenceClickListener(preference -> {
//            Intent intent = new Intent(getActivity(), SendActivity.class);
//            intent.putExtra(Constants.EXTRA_ADDRESS, Constants.DONATION_ADDRESS);
//            startActivity(intent);
//            return true;
//        });

        final Preference email = findPreference("pref_email");
        email.setOnPreferenceClickListener(preference -> {

            Intent mailto = new Intent(Intent.ACTION_SENDTO);
            mailto.setType("message/rfc822"); // use from live device
            mailto.setData(Uri.parse("mailto:support@trustwalletapp.com")
                    .buildUpon()
                    .appendQueryParameter("subject", "Android support question")
                    .appendQueryParameter("body", "Dear Trust support,")
                    .build());
            startActivity(Intent.createChooser(mailto, "Select email application."));
            return true;
        });
    }

    private void rateThisApp() {
    }

    private void setRpcServerPreferenceData(ListPreference lp) {
        CharSequence[] entries = new CharSequence[1];
        CharSequence[] entryValues = new CharSequence[1];
        entries[0] = "testnet";
        entryValues[0] = Constants.HOST_TESTNET;

        String currentValue = "testnet";
        lp.setEntries(entries);
        lp.setDefaultValue(currentValue);
        lp.setValue(currentValue);
        lp.setSummary(currentValue);
        lp.setEntryValues(entryValues);
    }

    private String getVersion() {
        return "1.0.0";
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {

    }
}
